﻿using System;
using System.Collections.Generic;
using System.Linq;
using wd2now.Framework.Bots;
using wd2now.Model.Table;
using Action = wd2now.Model.Actions.Action;

namespace wd2now.Framework.Controller
{
    public class TableController
    {
        public TableController()
        {
            Bots = new List<BotRegistration>();
        }

        protected List<BotRegistration> Bots;

        public void RegisterBot(IBot bot, int priority = 0)
        {
            var registeredBot = new BotRegistration
                {
                    Bot = bot,
                    Priority = priority,
                };

            Bots.Add(registeredBot);
        }

        public Action Play(Table table)
        {
            foreach (var botRegistration in Bots.OrderByDescending(b => b.Priority))
            {
                if (botRegistration.Bot.CanPlay(table))
                {
                    return botRegistration.Bot.Play(table);
                }
            }

            throw new Exception("Cannot find a bot that knows how to play this table. Consider registering a default bot with the lowest priority.");
        }
    }
}
